﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DAL;
namespace BAL
{
    public class BAL_PHIEUTHUEMAY
    {
        private static QuanLyTiemNetDataContext db = new QuanLyTiemNetDataContext();
        //thêm
        public static bool ThemPTM(ref string err,string MaKhach,float TongTien,float ChietKhau,float ThanhTien)
        {
            bool them = false;
            try
            {
                PHIEUTHUEMAY ptm = new PHIEUTHUEMAY();
                ptm.MaKhach = MaKhach; 
                ptm.TongTien = TongTien;
                ptm.ChietKhau = ChietKhau;
                ptm.ThanhTien = ThanhTien;
                ptm.DaXoa = false;
                db.PHIEUTHUEMAYs.InsertOnSubmit(ptm);
                db.SubmitChanges();
                
                them = true;
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return them;
        }
        //cập nhật
        public static bool CapNhatPTM(ref string err,int MaPhieu,string MaKhach,float TongTien,float ChietKhau,float ThanhTien,string NgayLapPhieu)
        {
            bool update = false;
            try
            {
                PHIEUTHUEMAY tim = db.PHIEUTHUEMAYs.Single(ptm => ptm.MaPhieu.Equals(MaPhieu));
                if(tim!=null)
                {
                    tim.MaKhach = MaKhach;
                    tim.TongTien = TongTien;
                    tim.ChietKhau = ChietKhau;
                    tim.ThanhTien = ThanhTien;
                    tim.NgayLapPhieu = NgayLapPhieu;
                    db.SubmitChanges();
                    update = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return update;
        }
        //xóa
        public static bool XoaPTM(ref string err,int MaPhieu)
        {
            bool xoa = false;
            try
            {
                PHIEUTHUEMAY tim = db.PHIEUTHUEMAYs.Single(ptm => ptm.MaPhieu.Equals(MaPhieu));
                if(tim!=null)
                {
                    db.PHIEUTHUEMAYs.DeleteOnSubmit(tim);
                    db.SubmitChanges();
                    xoa = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return xoa;
        }
        //đánh dấu xóa
        public static bool DanhDauXoa(ref string err,int MaPhieu,string value)
        {
            bool danhdauxoa = false;
            try
            {
                PHIEUTHUEMAY tim = db.PHIEUTHUEMAYs.Single(ptm => ptm.MaPhieu.Equals(MaPhieu));
                if(tim!=null)
                {
                    tim.DaXoa = true;
                    db.SubmitChanges();
                    danhdauxoa = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return danhdauxoa;
        }
        //ConvertToDataTable
        public static DataTable ConvertToDataTable(List<PHIEUTHUEMAY> list)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaPhieu");
            dt.Columns.Add("MaKhach");
            dt.Columns.Add("TongTien");
            dt.Columns.Add("ChietKhau");
            dt.Columns.Add("ThanhTien");
            //dt.Columns.Add("DaXoa");
            foreach(var i in list)
            {
                dt.Rows.Add(i.MaPhieu, i.MaKhach, i.TongTien, i.ChietKhau, i.ThanhTien);
            }
            return dt;
        }

        //lấy thông tin
        public static DataTable LayThongTinPTM()
        {
            var ptm = from pt in db.PHIEUTHUEMAYs where pt.DaXoa==false select pt;
            return ConvertToDataTable(ptm.ToList());
        }
        //tổng tiền
        public static float TinhTongTien(int maPhieu)
        {
            float tong=0;
            try
            {
                var bangtiendv = from t in db.CHITIETSUDUNGDVs where t.MaPhieu == maPhieu select t;
                DataTableReader dtr = BAL_CHITIETSUDUNGDV.ConvertToDataTable(bangtiendv.ToList()).CreateDataReader();
                while (dtr.Read())
                    tong += float.Parse(dtr["SoLuong"].ToString()) * float.Parse(dtr["DonGia"].ToString());
                dtr = null;
                var bangchitietthuemay = from tm in db.CHITIETTHUEMAYs where tm.MaPhieu == maPhieu select tm;
                dtr = BAL_CHITIETTHUEMAY.ConverToDataTable(bangchitietthuemay.ToList()).CreateDataReader();
                while (dtr.Read())
                    tong += float.Parse(BAL_CHITIETTHUEMAY.SoGioSuDung(maPhieu, dtr["MaMay"].ToString()).ToString()) * float.Parse(dtr["GiaThueMay"].ToString());
            }
            catch(Exception)
            {

            }
            return tong;
            
        }
        

    }
}
